package com.boboPlanet.comm.context;

import com.boboPlanet.comm.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.NamedThreadLocal;

/**
 * @Description: TODO 日志链路上下文
 * @Author: mis_wu
 * @Date: 2023/5/7
 * @Company planet
 **/
public class TraceIdContext {

    private static final String LOG_TRACE = "log_trace";

    private static final ThreadLocal<String> LOG_TRACE_MAP = new NamedThreadLocal<>(LOG_TRACE);

    public TraceIdContext(String traceId){
        LOG_TRACE_MAP.set(traceId);
    }


    public static String getTranceId(){
        return LOG_TRACE_MAP.get();
    }

    public static void setTraceId(String traceId){
        if (StringUtils.isBlank(traceId)){
            throw BusinessException.build("set tranceId error,tranceId is not null !!!");
        }
        LOG_TRACE_MAP.set(traceId);
    }

    public static void remove(){
        LOG_TRACE_MAP.remove();
    }

}
